package com.anjiplus.template.gaea.business.modules.quartz.service.impl;

import com.anjiplus.template.gaea.business.modules.quartz.service.OrgService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class orgServiceImpl implements OrgService {


    @Override
    public int insertOrg(String orgName, String orgCode) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        int orgInsert=0;
        //判断中间库是否已经存在，如已存在，不同步
        String selectSQL = "select count(1) as sum from yy_org_orgs where yyCode = '"+orgCode+"'";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(selectSQL);
        if(maps.size()>0){
            orgInsert = 0;
        }else{
            String orgSql = "INSERT INTO yy_org_orgs(name, yyCode) VALUES ("+orgName+","+orgCode+");\n";
            orgInsert = jdbcTemplate.update(orgSql);
        }
        return orgInsert;
    }

    @Override
    public String selectGsCode(String orgCode,JdbcTemplate jdbcTemplate) {
        String gsCode = "";
        //判断中间库是否已经存在，如已存在，不同步
        String selectSQL = "select gsCode from yy_org_orgs where yyCode = '"+orgCode+"'";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(selectSQL);
        if(maps.size()>0){
            gsCode = (String) maps.get(0).get("gsCode");
        }else{
            System.out.println("中间库未查询到ORGCODE对照关系！！");
        }
        return gsCode;
    }

    @Override
    public  List<Map<String, Object>>  selectZtCode(String orgCode, JdbcTemplate jdbcTemplate) {
        String yyztCode = "";
        //判断中间库是否已经存在，如已存在，不同步
        String selectSQL = "select name, yyCode, yyztCode, gsCode, gsztCode, kmtx, yyorgPK, yyztName, yyztPK, ztlxPK, ZTLXCODE, ZTLXNAME from yy_org_orgs where yyCode = '"+orgCode+"'";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(selectSQL);
        return maps;
    }

    @Override
    public  List<Map<String, Object>>  selectAllZtCode(JdbcTemplate jdbcTemplate) {
        String yyztCode = "";
        //判断中间库是否已经存在，如已存在，不同步
        String selectSQL = "select name, yyCode, yyztCode, gsCode, gsztCode, kmtx, yyorgPK, yyztName, yyztPK, ztlxPK, ZTLXCODE, ZTLXNAME from yy_org_orgs where gsCode is not null";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(selectSQL);
        return maps;
    }
}
